home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -in_the_mag- / program_perfection / memory.c < prev    next >
C/C++ Source or Header  |  1999-12-08  |  2KB  |  142 lines

  1. /*
  2.  * memory.c
  3.  *
  4.  * Replacement memory management routines (using pools)
  5.  *
  6.  * $Id :$
  7.  * $Log:$
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12. #include "exit.h"
  13. #include "memory.h"
  14.  
  15. #include <exec/memory.h>
  16. #include <proto/exec.h>
  17.  
  18.  
  19. /*******************************************************************/
  20.  
  21. /*
  22.  * #define MEMORY_LOG to list all allocations to a file
  23.  */
  24.  
  25. #ifdef MEMORY_LOG
  26.  
  27. #include <dos/dos.h>
  28. #include <proto/dos.h>
  29.  
  30. #ifndef MEMORY_LOG_FILE
  31. #define MEMORY_LOG_FILE "t:memory_log.txt"
  32. #endif
  33.  
  34. static BPTR LogFile;
  35.  
  36. /*
  37.  * MemLog_Dispose()
  38.  *
  39.  * Close the memory log. Called automatically at exit()
  40.  */
  41.  
  42. static VOID
  43. MemLog_Dispose( VOID )
  44. {
  45.     Close( LogFile );
  46. }
  47.  
  48.  
  49. #endif /* MEMORY_LOG */
  50.  
  51. /*******************************************************************/
  52.  
  53.  
  54. /*
  55.  * private stuff
  56.  */
  57.  
  58. static APTR MyPool;
  59.  
  60. #define MYPOOL_MEM_REQUIREMENTS     MEMF_ANY | MEMF_CLEAR
  61. #define MYPOOL_PUDDLE_SIZE          2048L
  62. #define MYPOOL_THRESH_SIZE          2048L
  63.  
  64. /*******************************************************************/
  65.  
  66.  
  67. /*
  68.  * MyPool_Dispose()
  69.  */
  70.  
  71. static VOID
  72. MyPool_Dispose( VOID )
  73. {
  74.     DeletePool( MyPool );
  75.  
  76.     return;
  77. }
  78.         
  79.  
  80. /*
  81.  * MyPool_New()
  82.  */
  83.  
  84. VOID
  85. _INIT_7_MyPool_New( VOID )
  86. {
  87.     if( MyPool = CreatePool( MYPOOL_MEM_REQUIREMENTS, MYPOOL_PUDDLE_SIZE, MYPOOL_THRESH_SIZE ) )
  88.         atexit( MyPool_Dispose );
  89.     else
  90.         MyExit( "Unable to allocate memory pool" );
  91.  
  92. #ifdef MEMORY_LOG
  93.  
  94.     if( LogFile = Open( MEMORY_LOG_FILE, MODE_NEWFILE ) )
  95.         atexit( MemLog_Dispose );
  96.     else
  97.         MyExit( "Unable to open memory log file:" MEMORY_LOG_FILE );
  98.  
  99. #endif /* MEMORY_LOG */
  100.  
  101.     return;
  102. }
  103.  
  104.  
  105. /*
  106.  * Memory_Alloc()
  107.  */
  108.  
  109. APTR
  110. Memory_Alloc( ULONG size )
  111. {
  112. #ifdef MEMORY_LOG
  113.  
  114.     APTR memory = AllocPooled( MyPool, size );
  115.     FPrintf( LogFile, "Got   %6ld bytes at %08lx\n", size, memory );
  116.     return memory;
  117.  
  118. #else
  119.  
  120.     return AllocPooled( MyPool, size );
  121.  
  122. #endif
  123. }
  124.  
  125.  
  126. /*
  127.  * Memory_Free()
  128.  */
  129.  
  130. VOID
  131. Memory_Free( APTR memory, ULONG size )
  132. {
  133. #ifdef MEMORY_LOG
  134.  
  135.     FPrintf( LogFile, "Freed %6ld bytes at %08lx\n", size, memory );
  136.  
  137. #endif
  138.  
  139.     FreePooled( MyPool, memory, size );
  140. }
  141.  
  142.